package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FContract;
import com.qf.contract.entity.FContractDetails;
import com.qf.contract.entity.FSigningUnit;
import com.qf.contract.entity.FTypeOfContract;
import com.qf.contract.mapper.FContractMapper;
import com.qf.contract.qo.ContractTerminationQueryQo;
import com.qf.contract.service.ContractTerminationQueryService;
import com.qf.contract.vo.ContractTerminationQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 合同终止服务查询
 */
@Slf4j
@Service
public class ContractTerminationQueryServiceImpl implements ContractTerminationQueryService {
    @Resource
    FContractMapper fContractMapper;

    /**
     * 全部合同状态查询
     *
     * @param contractTerminationQueryQo
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<ContractTerminationQueryVo> ContractTerminationQuery(ContractTerminationQueryQo contractTerminationQueryQo, int current, int size) {
        IPage<ContractTerminationQueryVo> page;
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContract.class)//查询user表全部字段
                    .selectAll(FContractDetails.class)
                    .selectAll(FTypeOfContract.class)
                    .selectAll(FSigningUnit.class)
                    .leftJoin(FContractDetails.class, FContractDetails::getContractId, FContract::getContractDetails)
                    .leftJoin(FTypeOfContract.class, FTypeOfContract::getContractId, FContract::getTypeOfContract)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit);

            if (contractTerminationQueryQo.getContractName() != null) {
                wrapper.like("contract_name", contractTerminationQueryQo.getContractName());
            }
            if (contractTerminationQueryQo.getContractNumber() != null) {
                wrapper.like("contract_number", contractTerminationQueryQo.getContractNumber());
            }
            /**
             * 合同状态
             */
            if (contractTerminationQueryQo.getContractStatus() != null) {
                wrapper.eq("current_link", contractTerminationQueryQo.getContractStatus());
            }
            if (contractTerminationQueryQo.getWorkArrivalTime() != null) {
                wrapper.eq("contract_stop_date", contractTerminationQueryQo.getWorkArrivalTime());
            }
//            if (contractTerminationQueryQo.getWorkArrivalTime() != null) {
//                wrapper.like("contract_number", contractTerminationQueryQo.getCrocessTitle())
//                        .or().like("operator", contractTerminationQueryQo.getCrocessTitle());
//            }
            //TODO 分页连表查询
            page = fContractMapper.selectJoinPage(new Page<>(current, size), ContractTerminationQueryVo.class, wrapper);
            //对查询的结果做处理
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return page;
    }


}
